// DefaultName.cpp

#include "StdAfx.h"

#include "DefaultName.h"

namespace Common
{
namespace FileSystem
{

static CString GetDefaultName3(const CString &fileName,
    const CString &extension, const CString &addSubExtension)
{
  int extLength = extension.GetLength();
  int fileNameLength = fileName.GetLength();
  if (fileNameLength > extLength + 1)
  {
    int dotPos = fileNameLength - (extLength + 1);
    if (fileName[dotPos] == _T('.'))
      if (extension.CompareNoCase(fileName.Mid(dotPos + 1)) == 0)
        return fileName.Left(dotPos) + addSubExtension;
  }
  int dotPos = fileName.ReverseFind(_T('.'));
  if (dotPos > 0)
    return fileName.Left(dotPos) + addSubExtension;

  if (addSubExtension.IsEmpty())
    return fileName + _T("~");
  else
    return fileName + addSubExtension;
}

CString GetDefaultName2(const CString &fileName,
    const CString &extension, const CString &addSubExtension)
{
  CString name = GetDefaultName3(fileName, extension, addSubExtension);
  name.TrimRight();
  return name;
}

}
}